package com.example.drive.student.service.impl;



import com.example.drive.common.ServiceCode;
import com.example.drive.common.ServiceException;
import com.example.drive.student.mapper.StudentMapper;
import com.example.drive.student.pojo.dto.UpdateStuDTO;
import com.example.drive.student.pojo.entity.Student;
import com.example.drive.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 获取学员
     * @return
     */
    @Override
    public List<Student> getList(){
        List<Student> studentList=studentMapper.getList();
        if(studentList==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在学员！");
        }
        return studentList;
    }

    /**
     * 修改学员信息
     * @param id
     * @param updateStuDTO
     */
    @Override
    public void update(Integer id, UpdateStuDTO updateStuDTO){
         Student student = studentMapper.getById(id);
        if(student==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该学员！");
        }
        student.setUsername(updateStuDTO.getUsername());
        student.setAge(updateStuDTO.getAge());
        student.setSex(updateStuDTO.getSex());
        student.setAddress(updateStuDTO.getAddress());
        student.setId_code(updateStuDTO.getId_code());
        student.setTel(updateStuDTO.getTel());
        int result = studentMapper.update(student);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败！");
        }
    }

    /**
     * 查询学员
     * @param id
     * @return
     */
    @Override
    public Student search(Integer id){
        Student student = studentMapper.getById(id);
        if(student==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该学员！");
        }
        return student;
    }


    /**
     * 更新分数
     * @param id
     * @param subject
     * @param newScore
     */
    @Override
    public void updateScore(Integer id, String subject, Integer newScore){
        Student student = studentMapper.getById(id);
        if(student==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该学员！");
        }
        if(subject.equals("km1")){
            if(!student.getReflag().equals(0)){
                student.setKm1(newScore);
            }
            else{
                throw new ServiceException(ServiceCode.ERR_EXISTS,"该学员未报名！");
            }
        }
        //科目二
        if(subject.equals("km2")){
            if(student.getKm1()>89){
                student.setKm2(newScore);
            }
            else{
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"科目一未通过！");
            }
        }
        //科目三
        if(subject.equals("km3")){
            if(student.getKm1()>89){
                student.setKm3(newScore);
            }
            else{
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"科目一未通过！");
            }

        }
        //科目四
        if(subject.equals("km4")) {
            if(student.getKm2()>79 && student.getKm3()>89){
                student.setKm4(newScore);
            }
            else{
                throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"科目二或科目三未通过！");
            }
        }
        int result = studentMapper.update(student);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败！");
        }
    }

    /**
     * 删除教练id
     * @param id
     */
    @Override
    public void refuse(Integer id){
        Student student = studentMapper.getById(id);
        if(student==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该学员！");
        }
        student.setCoach_id(null);
        int result = studentMapper.update(student);
        if(result<=0){
            throw new ServiceException(ServiceCode.ERR_SAVE_FAILED,"修改失败！");
        }

    }

}
